/**
 * @brief 将服务器的时间转化为日期时间
 * @param {*} date_from_server 服务器传来的时间
 * @return 日期和时间组
 */
function getDate(date_from_server) {
    var year = date_from_server.substring(12, 16);
    var month = date_from_server.substring(8, 11);
    var date = date_from_server.substring(5, 7);
    switch (month) {
        case "Jan":
            month = "01";
            break;
        case "Feb":
            month = "02";
            break;
        case "Mar":
            month = "03";
            break;
        case "Apr":
            month = "04";
            break;
        case "May":
            month = "05";
            break;
        case "Jun":
            month = "06";
            break;
        case "Jul":
            month = "07";
            break;
        case "Aug":
            month = "08";
            break;
        case "Sep":
            month = "09";
            break;
        case "Oct":
            month = "10";
            break;
        case "Nov":
            month = "11";
            break;
        case "Dec":
            month = "12";
            break;
    }
    return year + " " + month + "-" + date;
}

/**
 * @brief 将一串由空格和字符组成的字符串分割成字符串数组
 * @param {*} str 由空格和字符组成的字符串
 * @return 分隔完成的字符串数组
 */
function getDevidedString(str){
    var str_list = [];
    var index_begin;
    for(var i=0;i<str.length;i++){
        if (str[i + 1] != " " && str[i] == " ") {
            index_begin = i + 1;
        }
        if (str[i + 1] == " " && (str[i] != " " || i==str.length -1 )) {
            str_list.push(str.substring(index_begin, i + 1))
        }
    }
    if (str[str.length - 1] != " ") {
        str_list.push(str.substring(index_begin, str.length));
    }
    return str_list;
}

/**
 * @brief 初始化列表函数，在被处理列表后添加键值对 <<show:false>>
 * @param {*} list_name 列表名称的字符串
 * @param {*} that 需要传入this指针以操作data内容
 */
function matchInit(list_name, that) {
    for (var i = 0; i < that.data[list_name].length; i++) {
        that.setData({
            [list_name + "[" + i + "]" + ".show"]: false
        });
    }
}

/**
 * @brief 根据字符串判断列表选中与否，选中的添加键值对 <<show:true>>
 * @param {*} string_tobe_searched 待搜索内容
 * @param {*} list_name 要搜索的目标列表
 * @param {*} search_object 搜索的列表子内容
 * @param {*} that 需要传入this指针以操作data内容
 */
function matchString(string_tobe_searched, list_name, search_object, that) {
    var i, j;
    for (i = 0; i < that.data[list_name].length; i++) {
        for (j = 0; j < 1 + that.data[list_name][i][search_object].length - string_tobe_searched.length; j++) {
            if (that.data[list_name][i][search_object].substring(j, j + string_tobe_searched.length) == string_tobe_searched) {
                that.setData({
                    [list_name + "[" + i + "]" + ".show"]: true
                })
                break;
            }
        }
    }
}

/**
 * @brief 根据整数判断列表选中与否，选中的添加键值对 <<show:true>>
 * @param {*} int_tobe_searched 待搜索内容
 * @param {*} list_name 要搜索的目标列表
 * @param {*} search_object 搜索的列表子内容
 * @param {*} that 需要传入this指针以操作data内容
 */
function matchInt(int_tobe_searched, list_name, search_object, that) {
    var i;
    for (i = 0; i < that.data[list_name].length; i++) {
        if (int_tobe_searched == that.data[list_name][i][search_object]) {
            that.setData({
                [list_name + "[" + i + "]" + ".show"]: true
            });
        }
    }
}

module.exports = {
    getDate: getDate,
    getDevidedString:getDevidedString,
    matchInit: matchInit,
    matchString: matchString,
    matchInt: matchInt,
}
